package luke4fun.wheresmymoney.util;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import luke4fun.wheresmymoney.common.CommonConstants;

public class FormatUtil {

	private static SimpleDateFormat sdf = new SimpleDateFormat(CommonConstants.DATE_FORMAT);
	
	public static String formatDateToMMddyyyy(Date d) {
		return sdf.format(d);
	}
	
	public static Date parseDateFromMMddyyyy(String dateStr) {
		try {
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			Log.e("ZJ.FormatUtil", "date parse error");
			throw new RuntimeException(e);
		}
	}
	
	public static String formatNumber(long number) {
		StringBuffer tmp = new StringBuffer(String.valueOf(number));
		tmp = tmp.reverse();
		
		StringBuffer sb = new StringBuffer();
		int count = 0;
		for (int i=0; i<=tmp.length()-1; i++) {
			sb.append(tmp.charAt(i));
			if (++count%3 == 0) {
				sb.append(',');
			}
		}
		if (sb.charAt(sb.length()-1) == ',') {
			sb.setLength(sb.length()-1);
		}
		
		return sb.reverse().toString();
	}
}
